package com.demo.rabbitmq.direct;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

/**
 * @author sc
 * Created on  2018/9/16
 */
public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory cf = new ConnectionFactory();
        cf.setVirtualHost("/");
        cf.setHost("localhost");
        cf.setPort(5672);
        cf.setNetworkRecoveryInterval(3000);
        cf.setAutomaticRecoveryEnabled(true);

        Connection connection = cf.newConnection();
        Channel channel = connection.createChannel();
        String exchange = "test_direct_exchange";
        String routingKey =  "test.direct";

        //2 持久化模式 1 非持久化
        HashMap<String, Object> map = new HashMap<>();
        map.put("author","sc");
        AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().deliveryMode(2).appId("test")
                .contentEncoding("utf-8").expiration("10000")
                .headers(map).build();

        channel.basicPublish(exchange,routingKey,properties,"hello".getBytes());
    }
}
